<?php
    @session_start();
    require_once 'shared-functions.php';
    require_once 'session.php';
    require_once 'masterpage.php';

    if(!IsValidSession())
    {
        header('Location: login.php?page=index');
        exit();
    }
    else
    {
        RefreshSession();
    }

    $user = GetCurrentUserAccessLevel();

    //Check user access
    if($user != $DIRECTOR && $user != $ADMIN && $user != $PRINCIPLE)
    {
        header('Location: login.php?page=edit-semesters&error=To access the edit semesters page, please log in as a director, principle or admin&logout=1');
        exit();
    }

    masterpage("Edit Semesters");

    $semesterID = $_POST["semesterID"];
    $startDate = $_POST["datepicker1"];
    $endDate = $_POST["datepicker2"];
    $desc = $_POST["SemesterDescription"];
    $isSubmit = $_POST["isSubmit"];
    $newProgramID = $_POST["NewProgramID"];
    
    // A variable to store all the programs that are retrieved from the database.
    $programs = "";

    // If you are inserting data into the database, this will become false, and you 
    // will not see the form on this page.
    $showForm = true;

    if (isset($semesterID))
    {    
	$currentSemesterQuery = "SELECT `semesterID`,`StartDate`,`EndDate`,`Description`,`ProgramID` FROM `Semester` where semesterID = '$semesterID'";

	try
	{
			$link = connect_db();			

			// if the form has been submitted, insert data
			if ($isSubmit == 1)
			{
					$updateStartDate = ParseDateValues($startDate);
					$updateEndDate = ParseDateValues($endDate);
	
					
					$updateSemesterQuery = "UPDATE Semester SET StartDate = '$updateStartDate', EndDate = '$updateEndDate', Description = '$desc', ProgramID='$newProgramID' where semesterID ='$semesterID'";
					$result = mysql_query($updateSemesterQuery, $link);

					if($result===false)
					{
						$_SESSION['error']="Could not update semester. - edit-semesters.php";
						echo"<span style='color:red;'>Could not update the record in the database.</span>";
						return;
					}
					else
					{
					    $showForm = false;
					    echo 'Successfully Saved<br /><a href="manage-semesters.php">Click here</a> to go back to manage semesters.<br />';
					}
			}
			else
			{
				$result = mysql_query($currentSemesterQuery, $link);

				// Load the Semester from the ID that was passed in
				if($result===false)
				{
					$_SESSION['error']="Could not get the records from the database - edit-semesters.php";
					echo"<span style='color:red;'>Could not get the records from the database.</span>";
				}
				else
				{
				    while($semesterRow = mysql_fetch_array($result, MYSQL_ASSOC))
					{	
						$semesterID = $semesterRow["semesterID"];
						$programID = $semesterRow["ProgramID"];
						$startDate = $semesterRow["StartDate"];
						$endDate = $semesterRow["EndDate"];
						$desc = $semesterRow["Description"];
						$storedProgramID = $semesterRow["ProgramID"];
					}		
				}
			}		
		
		$programQuery = "select ProgramID, Name from Program";

		$programQueryResult = 	mysql_query($programQuery, $link);

	    	if($programQueryResult===false)
		{
			echo "hit";
			$_SESSION['error']="Could not get program details. - add-new-semester.php";
			return false;
		}
		else
		{
			while($currentProgramsRow = mysql_fetch_array($programQueryResult, MYSQL_ASSOC))
			{
				
				$programID = $currentProgramsRow["ProgramID"];
				$name = $currentProgramsRow["Name"];
				
				if (strcmp($storedProgramID,$programID) == 0)
				{
					$programs .= "<option selected='selected' value='$programID'>$name</option>";
				}
				else
				{
					$programs .= "<option value='$programID'>$name</option>";
				}
			}
		}
	}
	catch (Exception $exception)
	{
		$_SESSION['error']=$exception->getMessage();
		return false;
	}
		
?>

	<link type="text/css" href="jquery/css/overcast/jquery-ui-1.8.9.custom.css" rel="stylesheet" />	
	<script type="text/javascript" src="jquery/js/jquery-1.4.4.min.js"></script>
	<script type="text/javascript" src="jquery/js/jquery-ui-1.8.9.custom.min.js"></script>
	<script type="text/javascript">
		$(function(){

			// Datepicker1
			$('#datepicker1').datepicker({
				dateFormat: 'dd-mm-yy',
				width: '100px',
				inline: true,
				changeMonth: true,
				changeYear: true

			});

			// Datepicker2
			$('#datepicker2').datepicker({
				dateFormat: 'dd-mm-yy',
				inline: true,
				changeMonth: true,
				changeYear: true
			});		
		});
	</script>
	<style type="text/css">
		div.ui-datepicker{ font-size:12px;}
	</style>

	<?php 
		if ($showForm)
		{
	?>
   	<form action="edit-semesters.php" method="post">
	    <input type='hidden' name='semesterID' value="<?php echo$semesterID;?>"/>
	    <input type='hidden' name='isSubmit' value='1'>
	    <table class="DataList" style="width:500px;">
		<thead>
			<tr>
				<th class="header" colspan="2">Edit Semester</th>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td class="rows" colspan="2"> <span style="color:red;">All fields are required.</span></td>
			</tr>
			<tr>
				<td class="rows">Semester Description:</td>
				<td class="rows"><input type="text" name="SemesterDescription" ID="SemesterDescription" value="<?php echo$desc;?>"><span style="color:red;">&nbsp;*</span></td>
			</tr>
			<tr>
				<td>Program:</td>
				<td>
					<select name="NewProgramID" size="4" style="width:185px">
					<?php echo $programs; ?>
					</select>
					<span style="color:red;">*</span>
				</td>
			</tr>
			<tr>
				<td class="rows">Start Date:</td>
				<td class="rows"><input type="text" name="datepicker1" id="datepicker1" value="<?php echo DisplayDate($startDate);?>"><span style="color:red;">&nbsp;*</span></td>
			</tr>
			<tr>
				<td class="rows">End Date:</td>
				<td class="rows"><input type="text" name="datepicker2" id="datepicker2" value="<?php echo DisplayDate($endDate);?>"><span style="color:red;">&nbsp;*</span></td>
			</tr>
			<tr>
				<td class="rows" style="text-align:right;"><input type="submit" value="Submit"></td>
				<td class="rows"><input type="reset" value="Reset"></td>
			</tr>
		</tbody>
	    </table>
	</form>
	<?php
		}
	}
	else {
	    echo "You must first visit <a href='manage-semesters.php'>this page</a> to choose a semester.";
	}
	endmasterpage();
?>
